package com.example.demo1.user.view.controller;

import com.example.demo1.user.service.IUserService;
import com.example.demo1.user.service.impl.UserServiceImpl;
import com.example.demo1.user.view.application.LoginApplication;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

import java.io.IOException;

public class RegisterController {
    @FXML
    private TextField txtRegisterName;

    @FXML
    private PasswordField txtPassword;

    @FXML
    protected void doRegister() {
        String registerName = txtRegisterName.getText();
        String password = txtPassword.getText();

        IUserService IUserService = new UserServiceImpl();
        IUserService.insertUser(registerName, password);

        //关闭当前界面，加载新的界面
        Platform.runLater(() -> {
            //创建主界面窗口
            try {
                new LoginApplication().start(new Stage());
            } catch (IOException e) {
                e.printStackTrace();
            }
            //关闭注册窗口
            Stage stage = (Stage) txtRegisterName.getScene().getWindow();
            stage.close();
        });
    }

    @FXML
    protected void doCancel() {
        txtRegisterName.setText("");
        txtPassword.setText("");
    }

    @FXML
    protected void doBack() {
        //关闭当前界面，加载新的界面
        Platform.runLater(() -> {
            //创建主界面窗口
            try {
                new LoginApplication().start(new Stage());
            } catch (IOException e) {
                e.printStackTrace();
            }
            //关闭注册窗口
            Stage stage = (Stage) txtRegisterName.getScene().getWindow();
            stage.close();
        });
    }
}